package com.exartisan.util;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.exartisan.adapter.CommentsAdapter;
import com.exartisan.bean.FbComment;
import com.exartisan.network.FacebookResponseParser;
import com.exartisan.network.FbConnnector;
import com.exartisan.network.NetworkUtility;
import com.exartisan.thanhdoanhn.R;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.PauseOnScrollListener;

public class CommentDialog {

	private static TextView tvRetry;
	private static ProgressBar pBar;
	private static View layoutContent;
	private static ListView jobList;
	private static Activity context;
	
	public interface onCommentListener{
		public void onSuccess();
	}

	public static void show(final Activity context, final String id) {
		CommentDialog.context = context;
		final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(context);
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		final View popupLayout = inflater.inflate(R.layout.activity_comment,
				null);
		layoutContent = popupLayout.findViewById(R.id.layoutContent);
		pBar = (ProgressBar) popupLayout.findViewById(R.id.progressBar);
		tvRetry = (TextView) popupLayout.findViewById(R.id.tvRetry);
		final RelativeLayout btnSend = (RelativeLayout) popupLayout
				.findViewById(R.id.layout_send);
		jobList = (ListView) popupLayout.findViewById(R.id.lv_Comments);
		final EditText edtComment = (EditText) popupLayout
				.findViewById(R.id.edtComment);
		final ProgressBar pBar = (ProgressBar) popupLayout.findViewById(R.id.progressBarSend);

		helpBuilder.setView(popupLayout);
		final AlertDialog helpDialog = helpBuilder.create();
		helpDialog.show();
		
		jobList.setOnScrollListener(new PauseOnScrollListener(ImageLoader.getInstance(), true, true));
		
		getFbComments(id);
		
		btnSend.setOnClickListener(new OnClickListener() {
			

			@Override
			public void onClick(View v) {
				if (edtComment.length()<1) {
					Util.showToast("Bạn chưa nhập bình luận", context, Toast.LENGTH_SHORT);
					return;
				}
				
				btnSend.setVisibility(View.INVISIBLE);
				pBar.setVisibility(View.VISIBLE);
				
				FbUtil.commentOnId(edtComment.getText().toString(), id, context, new AsyncHttpResponseHandler(){
					
					@Override
					public void onSuccess(int arg0, String arg1) {
						btnSend.setVisibility(View.VISIBLE);
						pBar.setVisibility(View.GONE);
						edtComment.setText("");
						super.onSuccess(arg0, arg1);
						
						InputMethodManager inputMethodManager = (InputMethodManager)  context.getSystemService(Activity.INPUT_METHOD_SERVICE);
					    inputMethodManager.hideSoftInputFromWindow(popupLayout.getWindowToken(), 0);
						getFbComments(id);
						
					}
					
					@Override
					public void onFailure(Throwable arg0, String arg1) {
						btnSend.setVisibility(View.GONE);
						pBar.setVisibility(View.VISIBLE);
						
						super.onFailure(arg0, arg1);
						
						Util.showToast("Không thể đăng bình luận", context, Toast.LENGTH_SHORT);
					}
					
				},new OnCancelAuthenticate() {
					
					@Override
					public void CancelAuthent() {
						
						pBar.setVisibility(View.GONE);
						btnSend.setVisibility(View.VISIBLE);
						Util.showToast("Không thể đăng bình luận", context, Toast.LENGTH_SHORT);
					}
				});
			}
			
			});
		tvRetry.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getFbComments(id);
			}
		});

	}
	
	public static void show(final Activity context, final String id, final onCommentListener listener) {
		CommentDialog.context = context;
		final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(context);
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		final View popupLayout = inflater.inflate(R.layout.activity_comment,
				null);
		layoutContent = popupLayout.findViewById(R.id.layoutContent);
		pBar = (ProgressBar) popupLayout.findViewById(R.id.progressBar);
		tvRetry = (TextView) popupLayout.findViewById(R.id.tvRetry);
		final RelativeLayout btnSend = (RelativeLayout) popupLayout
				.findViewById(R.id.layout_send);
		jobList = (ListView) popupLayout.findViewById(R.id.lv_Comments);
		final EditText edtComment = (EditText) popupLayout
				.findViewById(R.id.edtComment);
		final ProgressBar pBar = (ProgressBar) popupLayout.findViewById(R.id.progressBarSend);

		helpBuilder.setView(popupLayout);
		final AlertDialog helpDialog = helpBuilder.create();
		helpDialog.show();
		
		jobList.setOnScrollListener(new PauseOnScrollListener(ImageLoader.getInstance(), true, true));
		
		getFbComments(id);
		
		btnSend.setOnClickListener(new OnClickListener() {
			

			@Override
			public void onClick(View v) {
				if (edtComment.length()<1) {
					Util.showToast("Bạn chưa nhập bình luận", context, Toast.LENGTH_SHORT);
					return;
				}
				
				btnSend.setVisibility(View.INVISIBLE);
				pBar.setVisibility(View.VISIBLE);
				
				FbUtil.commentOnId(edtComment.getText().toString(), id, context, new AsyncHttpResponseHandler(){
					
					@Override
					public void onSuccess(int arg0, String arg1) {
						btnSend.setVisibility(View.VISIBLE);
						pBar.setVisibility(View.GONE);
						
						super.onSuccess(arg0, arg1);
						edtComment.setText("");
						listener.onSuccess();
						InputMethodManager inputMethodManager = (InputMethodManager)  context.getSystemService(Activity.INPUT_METHOD_SERVICE);
					    inputMethodManager.hideSoftInputFromWindow(popupLayout.getWindowToken(), 0);
						getFbComments(id);
						
					}
					
					@Override
					public void onFailure(Throwable arg0, String arg1) {
						btnSend.setVisibility(View.GONE);
						pBar.setVisibility(View.VISIBLE);
						
						super.onFailure(arg0, arg1);
						
						Util.showToast("Không thể đăng bình luận", context, Toast.LENGTH_SHORT);
					}
					
				}, new OnCancelAuthenticate() {
					
					@Override
					public void CancelAuthent() {
						pBar.setVisibility(View.GONE);
						btnSend.setVisibility(View.VISIBLE);
						
					}
				});
			}
			
			});
		tvRetry.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getFbComments(id);
			}
		});

	}

	private static void getFbComments(final String id) {
		if (NetworkUtility.checkNetworkState(context)) {
			tvRetry.setVisibility(View.GONE);
			pBar.setVisibility(View.VISIBLE);
			layoutContent.setVisibility(View.INVISIBLE);

			FbConnnector.get(id, NetworkUtility.CONNECTION_COMMENT, new RequestParams("access_token", FbUtil.ACCESS_TOKEN),
					new AsyncHttpResponseHandler() {

						@Override
						public void onSuccess(int arg0, String response) {
							if (response == null) {
								tvRetry.setVisibility(View.VISIBLE);
								pBar.setVisibility(View.GONE);
								layoutContent.setVisibility(View.INVISIBLE);
							}
							super.onSuccess(arg0, response);
							Log.d("fbRes", response.toString());
							ArrayList<FbComment> comments = FacebookResponseParser
									.getComments(response);
							tvRetry.setVisibility(View.GONE);
							pBar.setVisibility(View.GONE);
							layoutContent.setVisibility(View.VISIBLE);
							Log.e("TEST COMMENT: ",""+ comments.size());
							jobList.setAdapter(new CommentsAdapter(comments,
									context));
						}

						@Override
						public void onFailure(Throwable arg0, String arg1) {
							super.onFailure(arg0, arg1);
							tvRetry.setVisibility(View.VISIBLE);
							pBar.setVisibility(View.GONE);
							layoutContent.setVisibility(View.INVISIBLE);
						}

					});

		} else {
			tvRetry.setVisibility(View.VISIBLE);
			pBar.setVisibility(View.GONE);
			layoutContent.setVisibility(View.INVISIBLE);
		}

	}

}
